package com.gm.wj.service;

import com.gm.wj.dao.AdminRoleDAO;
import com.gm.wj.entity.*;
import com.gm.wj.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Evan
 * @date 2019/11
 */
@Slf4j
@Service
public class AdminRoleService {
    @Autowired
    AdminRoleDAO adminRoleDAO;
    @Autowired
    UserService userService;
    @Autowired
    AdminUserRoleService adminUserRoleService;
    @Autowired
    AdminPermissionService adminPermissionService;
    @Autowired
    AdminRolePermissionService adminRolePermissionService;
    @Autowired
    AdminMenuService adminMenuService;

    public List<AdminRole> listWithPermsAndMenus(int i) {
        // 查询已启用的角色
        List<AdminRole> roles = null;
        if (i == 1) {
            // 查询已启用的角色
            roles = adminRoleDAO.findAllByEnabled();
        } else {
            // 查询全部的角色
            roles = adminRoleDAO.findAll();
        }

        for (AdminRole role : roles) {  // 4
            List<AdminPermission> perms = adminPermissionService.listPermsByRoleId(role.getId());
            List<AdminMenu> menus = adminMenuService.getMenusByRoleId(role.getId());
            role.setPerms(perms);
            /**
             * 因为数组中存的对象是引用数据类型  menus只是被引用，如果被更改了，后续的角色SetMenus的引用地址也会被影响
             * ！！！
             * java 数组对象深拷贝  解决了role角色菜单信息的混乱问题
             */
            String json = JsonUtils.objectToJson(menus);
            role.setMenus(JsonUtils.jsonToList(json, AdminMenu.class));

        }
        return roles;
    }

    public void addOrUpdate(AdminRole adminRole) {
        adminRoleDAO.save(adminRole);
    }

    public List<AdminRole> listRolesByUser(String username) {
        int uid = userService.findByUsername(username).getId();
        List<Integer> rids = adminUserRoleService.listAllByUid(uid)
                .stream().map(AdminUserRole::getRid).collect(Collectors.toList());
        return adminRoleDAO.findAllById(rids);
    }

    public AdminRole updateRoleStatus(AdminRole role) {
        AdminRole roleInDB = adminRoleDAO.findById(role.getId());
        roleInDB.setEnabled(role.isEnabled());
        return adminRoleDAO.save(roleInDB);
    }

    public void editRole(@RequestBody AdminRole role) {
        adminRoleDAO.save(role);
        adminRolePermissionService.savePermChanges(role.getId(), role.getPerms());
    }

    public void deleteById(int id) {
        adminRoleDAO.deleteById(id);
    }

    // 查询role是否已经存在
    public int findByRoleName(String roleName) {
        roleName = (roleName==null)?null:"%"+roleName+"%";
        return adminRoleDAO.findByRoleName(roleName);
    }

    public  List<AdminRole> findByQueryKey(String queryKey1, String queryKey2 ) {

        // 多条件模糊查询
        queryKey1 = (queryKey1==null)?null:"%"+queryKey1+"%";
        queryKey2 = (queryKey2==null)?null:"%"+queryKey2+"%";
        List<AdminRole> roles = adminRoleDAO.find(queryKey1,queryKey2);
        log.info(roles.toString());

        List<AdminPermission> perms;
        List<AdminMenu> menus;
        for (AdminRole role : roles) {
            perms = adminPermissionService.listPermsByRoleId(role.getId());
            menus = adminMenuService.getMenusByRoleId(role.getId());
            role.setPerms(perms);
            role.setMenus(menus);
        }
        return roles;
    }

    public AdminRole findById(int id) {
        return adminRoleDAO.findById(id);
    }
}
